﻿using System;
using System.Text;
using TI_BLE_HCI_ClientLib.BaseEntities;
using TI_BLE_HCI_ClientLib.GenericHCIEntities;
using TI_BLE_HCI_ClientLib.HCIBaseEntities;

namespace TI_BLE_HCI_ClientLib.HCICommands
{
    public class HCICommand_GATT_Handle_Value_Indication : HCIVendorSpecificCommand
    {
        private const byte CMD = 0x1D;

        public HCICommand_GATT_Handle_Value_Indication(ushort connHandle, ushort attributeHandle, byte[] value, bool authenticatedLinkRequired = false)
            : base(HCIVendorSpecificCommandCSG.ATT, CMD)
        {
            var pars = new BaseEntityParameter[4];

            pars[0] = new WordParameter(connHandle);
            pars[1] = new ByteParameter(authenticatedLinkRequired ? (byte)0x01 : (byte)0x00);
            pars[2] = new WordParameter(attributeHandle);
            pars[3] = new ByteArrayParameter(value);

            base.addParameters(pars);
        }

        public HCICommand_GATT_Handle_Value_Indication(ushort connHandle, ushort attributeHandle, byte value, bool authenticatedLinkRequired = false)
            : this(connHandle, attributeHandle, new byte[] { value }, authenticatedLinkRequired) { }

        public HCICommand_GATT_Handle_Value_Indication(ushort connHandle, ushort attributeHandle, ushort value, bool authenticatedLinkRequired = false)
            : this(connHandle, attributeHandle, new byte[] { (byte)(value & 0xFF), (byte)((value >> 8) & 0xFF) }, authenticatedLinkRequired) { }

        public HCICommand_GATT_Handle_Value_Indication(ushort connHandle, ushort attributeHandle, string value, bool authenticatedLinkRequired = false)
            : this(connHandle, attributeHandle, Encoding.UTF8.GetBytes(value), authenticatedLinkRequired) { }
    }
}
